import requests, asyncio, json, logging, mySecurity
from datetime import datetime

def set_node():
    global __node
    url_node = 'http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodes'
    try:
        r = requests.get(url_node)
        r.raise_for_status()
        __node = r.text
        __node = __node.replace("\\'", "'")
        __node = json.loads(__node)
        __node = __node[1][0][1]
        return True
    except Exception as e:
        logging.exception('')

    return False


can_clock_four = True

def clock_four():
    global can_clock_four
    if can_clock_four == True:
        if time.hour >= 4:
            set_node()
            can_clock_four = False
        if can_clock_four == False:
            if time.hour < 4:
                can_clock_four = True


def get_node():
    if len(__node) >= 9:
        nodes = __node[4:7]
        nodes.append(__node[8])
        return nodes
    else:
        return 'null'


async def cycle():
    global time
    await asyncio.sleep(1)
    while True:
        time = datetime.now()
        set_node()
        await asyncio.sleep(3600)